library

let’s load some libraries needed

##library
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.4     v stringr 1.4.0
## v readr   2.0.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(hrbrthemes)
## Warning: 程辑包'hrbrthemes'是用R版本4.1.2 来建造的
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(plotly)
## Warning: 程辑包'plotly'是用R版本4.1.2 来建造的
## 
## 载入程辑包:'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(viridis)
## Warning: 程辑包'viridis'是用R版本4.1.2 来建造的
## 载入需要的程辑包:viridisLite
## the dataset is provided by gapminder library
library(gapminder)

data wringling

let’s keep data for 2007 only

data <- gapminder %>% filter(year=='2007') %>% select(-year)

##let’s build a chart

build chart with ggplot, make it interactive with plotly

p <- data %>% 
  mutate(gdpPercap=round(gdpPercap,0)) %>% 
  mutate(pop=round(pop/1000000,2)) %>% 
  mutate(lifeExp=round(lifeExp,1)) %>% 
  arrange(desc(pop)) %>% 
  mutate(country=factor(country,country)) %>% 
  mutate(text=paste('country:',country,'\npopulation:',pop,'\nlife expectancy:',lifeExp,'\nGDP per capita:',gdpPercap,sep = '')) %>% 
  ggplot(aes(x=gdpPercap,y=lifeExp,size=pop,color=continent,text=text))+
    geom_point(alpha=0.7)+
    scale_size(range = c(1.4,19),name = 'population')+
    scale_color_viridis(discrete = T,guide=F)+
    theme_ipsum()+
    theme(legend.position = 'none')

ggplotly(p,tooltip = 'text')

Figure: Here is a really important caption.

 

A work by Zhu Yang

zy1794148812@gmail.com